(function() {
    angular.module("webApp").controller("CustomformCtrl", CustomformCtrl);
    CustomformCtrl.$inject = ['$scope', 'dataService', 'helper', '$state', '$location', '$rootScope', 'sessionService'];

    function CustomformCtrl($scope, dataService, helper, $state, $location, $rootScope, sessionService) {
        $scope.listAction = function() {
            $scope.pageSize = 10;
            $scope.getCustomFormList(1);
        }

        $scope.getCustomFormList = function(pageNo) {
            var query = {
                page_no: pageNo,
                page_size: $scope.pageSize,
                store_id: $rootScope.store_id
            };
            dataService.customformGet(query).then(function(result) {
                if (result.total_count) {
                    $scope.customform_list = result.customform_list;
                    $scope.total_count = result.total_count;
                }
            });
        }

        //新建和编辑配置页面
        $scope.addAction = function() {
            $scope.form_id = $location.search().form_id;
            if ($scope.form_id) {
                var query = {
                    "store_id": $rootScope.store_id,
                    "form_id": $scope.form_id
                };
                dataService.customformGet(query).then(function(result) {
                    if (result.total_count) {
                        $scope.custom_form = result.customform_list[0];
                    } else {
                        helper.myAlert("error：" + result.Msg);
                        return false;
                    }
                });
            } else {
                $scope.custom_form = {};
                $scope.custom_form.store_id = $rootScope.store_id;
                $scope.custom_form.form_name = "";
                $scope.custom_form.form_msg = "";
                $scope.custom_form.display = 1;
            }
        }

        $scope.customformAdd = function() {
            if (!$scope.custom_form.form_name) {
                helper.myAlert("表单名称不能为空");
                return false;
            }
            if (!$scope.custom_form.form_msg) {
                helper.myAlert("表单说明不能为空");
                return false;
            }

            dataService.customformAdd($scope.custom_form).then(function(result) {
                if (result.Msg != "ok") {
                    helper.myAlert("error:" + result.Msg);
                } else {
                    helper.myAlert("success", null, null, function() {
                        $state.go("customform.list");
                    });
                }
            });
        }

        $scope.form_types = [{
                type: "text",
                name: "单行文本(text)"
            },
            {
                type: "number",
                name: "单行文本(number)"
            },
            {
                type: "textarea",
                name: "多行文本(textarea)"
            },
            {
                type: "password",
                name: "密码框(password)"
            },
            {
                type: "checkbox",
                name: "多选框(checkbox)"
            },
            {
                type: "select",
                name: "下拉框(select)"
            },
            {
                type: "radio",
                name: "单选框(radio)"
            },
            {
                type: "date",
                name: "日期(datetime)"
            },
            {
                type: "riqi",
                name: "年月日(date)"
            },
            {
                type: "image",
                name: "图片上传(image)"
            },
            {
                type: "area",
                name: "选择地区(area)"
            },
            {
                type: "idcard",
                name: "身份证号(idcard)"
            },
            {
                type: "digit",
                name: "数字键盘带小数点(digit)"
            },
            {
                type: "mobile",
                name: "手机号(mobile)"
            },
            {
                type: "remark",
                name: "备注(remark)"
            },
            {
                type: "hide",
                name: "隐藏字段[保存后可见](hide)",
            }
        ];

        //表单选项列表
        $scope.typelistAction = function() {
            $scope.form_id = $location.search().form_id;
            if ($scope.form_id) {
                var query = {
                    "store_id": $rootScope.store_id,
                    "form_id": $scope.form_id
                };
                dataService.customformGet(query).then(function(result) {
                    if (result.total_count) {
                        $scope.custom_form = result.customform_list[0];
                    } else {
                        helper.myAlert("error：" + result.Msg);
                        return false;
                    }
                });
            }
            $scope.form_type = {};

            $scope.form_type.store_id = $rootScope.store_id;
            $scope.form_type.form_id = $scope.form_id;
            $scope.form_type.title = "";
            $scope.form_type.title_msg = "";
            $scope.form_type.default_value = "";
            $scope.form_type.sort = 0;
            $scope.form_type.is_must = 1;
            $scope.form_type.options = [{
                "option_name": "默认值",
                "option_value": 0
            }];

            $scope.form_type.status = 1;

            //选项类型 默认选中text
            $scope.form_type.type = "text";
            // $scope.form_types = [
            //     { type: "text", name: "单行文本(text)" },
            //     { type: "number", name: "单行文本(number)" },
            //     { type: "textarea", name: "多行文本(textarea)" },
            //     { type: "password", name: "密码框(password)" },
            //     { type: "checkbox", name: "多选框(checkbox)" },
            //     { type: "radio", name: "单选框(radio)" },
            //     { type: "date", name: "日期(datetime)" },
            //     { type: "image", name: "图片上传(image)" },
            //     { type: "area", name: "选择地区(area)" },
            //     { type: "idcard", name: "身份证号(idcard)" },
            //     { type: "digit", name: "数字键盘带小数点(digit)" },
            // ];

            //选项列表
            $scope.pageSize = 10;
            $scope.getCustomFormTypeList(1);
        }

        $scope.getCustomFormTypeList = function(pageNo) {
            var query = {
                page_no: pageNo,
                page_size: $scope.pageSize,
                store_id: $rootScope.store_id,
                form_id: $scope.form_id
            };
            dataService.customformTypeGet(query).then(function(result) {
                if (result.total_count) {
                    customform_type_list = result.customform_type_list;
                    for (var i in customform_type_list) {
                        if (customform_type_list[i].type == "remark") {
                            customform_type_list[i].default_value = json2html(JSON.parse(customform_type_list[i].default_value));
                        }
                    }
                    $scope.customform_type_list = customform_type_list;
                    $scope.total_count = result.total_count;
                }
            });
        }

        //选项添加
        $scope.customformTypeAdd = function() {
            if (!$scope.form_type.title) {
                helper.myAlert("选项名称不能为空");
                return false;
            }

            if (!$scope.form_type.form_id) {
                helper.myAlert("没有选择对应的自定义表单");
                return false;
            }

            if ($scope.form_type.type == "checkbox" || $scope.form_type.type == "radio" || $scope.form_type.type == "select") {

            } else {
                $scope.form_type.options = "";
            }
            // console.error($scope.form_type);
            // return false;

            if ($scope.form_type.type == "remark") {
                $scope.form_type.default_value = JSON.stringify(html2json($scope.form_type.default_value1));
            }

            if ($scope.form_type.type == "hide") {
                $scope.form_type.default_value = JSON.stringify(html2json($scope.form_type.default_value1));
            }

            dataService.customformTypeAdd($scope.form_type).then(function(result) {
                if (result.Msg != "ok") {
                    helper.myAlert("error:" + result.Msg);
                    return false;
                } else {
                    if ($scope.form_type.type_id) {
                        helper.myAlert("success", "#/customform/typelist?form_id=" + $scope.form_type.form_id);
                    } else {
                        helper.myAlert("success");
                        $scope.getCustomFormTypeList(1);

                        $scope.form_type.title = "";
                        $scope.form_type.title_msg = "";
                        $scope.form_type.options = [{
                            "option_name": "默认值",
                            "option_value": 0
                        }];
                        $scope.form_type.default_value = "";
                        $scope.form_type.sort = 0;
                    }
                }
            });
        }

        $scope.typeaddAction = function() {
            $scope.type_id = $location.search().type_id;
            if ($scope.type_id) {
                var query = {
                    "store_id": $rootScope.store_id,
                    "type_id": $scope.type_id
                };
                dataService.customformTypeGet(query).then(function(result) {
                    if (result.total_count) {
                        $scope.form_type = result.customform_type_list[0];

                        // if ($scope.form_type.type != "checkbox" || $scope.form_type.type != "radio" || $scope.form_type.type != "select") {
                        //     $scope.form_type.options = [{
                        //         "option_name": "默认值",
                        //         "option_value": 0
                        //     }];
                        // }
                        if ($scope.form_type.type == "remark") {
                            $scope.form_type.default_value1 = json2html(JSON.parse($scope.form_type.default_value));
                        }
                        if ($scope.form_type.type == "hide") {
                            $scope.form_type.default_value1 = json2html(JSON.parse($scope.form_type.default_value));
                        }

                    } else {
                        helper.myAlert("error：" + result.Msg);
                        return false;
                    }
                });
            }
        }

        //增加选项
        $scope.addOptions = function() {
            $scope.form_type.options.push({
                "option_name": "默认值" + Math.floor(Math.random() * 100),
                "option_value": Math.floor(Math.random() * 100 + 1)
            });
        }

        //删除选项
        $scope.delOption = function(index) {
            if ($scope.form_type.options.length == 1) {
                helper.myAlert("error：选项至少添加一项！");
                return false;
            } else {
                delete $scope.form_type.options.splice(index, 1);
            }
        }

        //数据列表
        $scope.datalistAction = function() {
            $scope.form_id = $location.search().form_id;
            $scope.type = $location.search().type;

            $scope.page_size = 10;
            $scope.dataList(1);
        }

        $scope.dataList = function(page_no) {
            var query = {
                page_no: page_no,
                page_size: $scope.page_size,
                form_id: $scope.form_id,
                type: $scope.type,
                store_id: $rootScope.store_id
            };
            dataService.formDataGet(query).then(function(result) {
                if (result.total_count) {
                    var customform_data = result.customform_data;
                    var data = [];
                    for (var j in customform_data) {
                        var a = customform_data[j].form_data;
                        if (a) {
                            var title = [];
                            for (var i in a) {
                                if (a[i].type == "area") {
                                    var option = a[i].option;
                                    a[i].option = option.join(" ");
                                }
                                if (a[i].type == "radio") {
                                    var option1 = a[i].option;
                                    var options1 = a[i].options;
                                    for (var j in options1) {
                                        if (options1[j].option_value == option1) {
                                            a[i].option = options1[j].option_name;
                                        }
                                    }
                                }
                                if (a[i].type == "checkbox") {
                                    var option2 = a[i].option;
                                    var options2 = a[i].options;
                                    a[i].option = "";
                                    for (var j in options2) {
                                        if (option2.indexOf(options2[j].option_value) > -1) {
                                            a[i].option += options2[j].option_name + " ";
                                        }
                                    }
                                }
                                title.push(a[i].title);
                            }

                            var b = {
                                data: a
                            }
                            data.push(b);
                        }
                    }
                    $scope.form_title = title;
                    $scope.form_data = data;
                    $scope.data_count = result.total_count;
                }
            });
        }
    }
})();